package java08;
//定义SaleThread3类实现Runnable接口
class Salethread3 implements Runnable{
    private int tickets = 10;
    public void run(){
        while(true){
            try {
                Thread.sleep(100);//模拟售票耗时过程
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            saleTicket();
        }
    }
    private synchronized  void saleTicket(){//定义一个同步方法saleTicket()
        if(tickets > 0){
            System.out.println(Thread.currentThread().getName() + "正在发售第" + tickets-- + "张票");
        }
    }
}
public class LockTest02 {
    public static void main(String[] args){
        Salethread3 salethread = new Salethread3();
        //创建并开启4个线程，模拟4个售票窗口
        new Thread(salethread,"窗口 1").start();
        new Thread(salethread,"窗口 2").start();
        new Thread(salethread,"窗口 3").start();
        new Thread(salethread,"窗口 4").start();
    }
}
